#include<bits/stdc++.h>
using namespace std;
int n,a[200005],c[200005],ans=0;
void dfs(int dep){
    if (dep>n){
        int ls1=-1,ls2=-1,sum=0;
        for (int i=1;i<=n;i++){
            if (c[i]==1){
                if (ls1==-1) ls1=i;
                else {
                    if (a[i]==a[ls1]) sum+=a[i];
                    ls1=i;
                }
            }
            else {
                if (ls2==-1) ls2=i;
                else {
                    if (a[i]==a[ls2]) sum+=a[i];
                    ls2=i;
                }
            }
        }
        ans=max(ans,sum);
        return;
    }
    c[dep]=1;
    dfs(dep+1);
    c[dep]=2;
    dfs(dep+1);
}
int t[15],co[15];
void DFS(int dep){
    if (dep>10){
        for (int i=1;i<=n;i++)
            c[i]=co[a[i]];
        int ls1=-1,ls2=-1,sum=0;
        for (int i=1;i<=n;i++){
            if (c[i]==1){
                if (ls1==-1) ls1=i;
                else {
                    if (a[i]==a[ls1]) sum+=a[i];
                    ls1=i;
                }
            }
            else {
                if (ls2==-1) ls2=i;
                else {
                    if (a[i]==a[ls2]) sum+=a[i];
                    ls2=i;
                }
            }
        }
        ans=max(ans,sum);
        return;
        
    }
    if (t[dep]==0) dfs(dep+1);
    co[dep]=1;
    DFS(dep+1);
    co[dep]=2;
    DFS(dep+1);
}
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    srand(time(0));
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        bool f=1;
        for (int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            if (a[i]>10) f=0;
        }
        if (n<=15){
            memset(c,0,sizeof(c));
            ans=0;
            dfs(1);
            printf("%d\n",ans);
            continue;
        }
        if (f){
            for (int i=1;i<=n;i++)
                t[a[i]]++;
            ans=0;
            memset(c,0,sizeof(c));
            memset(co,0,sizeof(co));
            DFS(1);
            printf("%d\n",ans);
            continue;
        }
        ans=0;
        for (int t=1;t<=2000000/n;t++){
            for (int i=1;i<=n;i++)
                c[i]=rand()%2+1;
            int ls1=-1,ls2=-1,sum=0;
            for (int i=1;i<=n;i++){
                if (c[i]==1){
                    if (ls1==-1) ls1=i;
                    else {
                        if (a[i]==a[ls1]) sum+=a[i];
                        ls1=i;
                    }
                }
                else {
                    if (ls2==-1) ls2=i;
                    else {
                        if (a[i]==a[ls2]) sum+=a[i];
                        ls2=i;
                    }
                }
            }
            ans=max(ans,sum);
        }
        printf("%d\n",ans);
    }
    return 0;
}